package com.hiof.overlays;

import android.content.res.Resources;
import android.graphics.Bitmap;

import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.widget.Toast;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.hiof.Configuration;
import com.hiof.R;
import com.hiof.servercommunication.background.job.listener.ITeamMemberLocationListener;
import com.hiof.servercommunication.receive.beans.GeoPoint;
import com.hiof.servercommunication.receive.beans.MemberLocationInfo;
import com.hiof.servercommunication.receive.beans.Packet;
import com.hiof.servercommunication.receive.beans.TeamMemberLocationPacket;
import com.hiof.utility.LocationUtils;
import com.hiof.utility.Utility;

import java.util.List;

public class TeammatePositionOverlay extends Overlay implements ITeamMemberLocationListener {
    Resources resources;
    Bitmap bmp;
    TeamMemberLocationPacket teammateLocationPacket;
    public TeammatePositionOverlay(Resources resources) {
        this.resources = resources;
        teammateLocationPacket = new TeamMemberLocationPacket();
    }


    @Override
    public void draw(Canvas canvas, MapView mapView, boolean b) {
        super.draw(canvas, mapView, false);
        drawTeammatesLocation(canvas, mapView);
    }

    private void drawTeammatesLocation(Canvas canvas, MapView mapView) {
        int markerID = R.drawable.teammate;
        Point screenPt = new Point();
        for(MemberLocationInfo memberLocationInfo: teammateLocationPacket.getTeamMemberLocations()){
            mapView.getProjection().toPixels(LocationUtils.getGoogleGeoPointsFromLocalGeoPoint(memberLocationInfo.getGeoPoint()), screenPt);
            drawMarker(canvas, markerID, resources, screenPt.x, screenPt.y);
        }
    }

    public static void drawMarker(Canvas canvas, int id, Resources resources,
                                  int x, int y) {
        Bitmap bitmap = BitmapFactory.decodeResource(resources, id);
        canvas.drawBitmap(bitmap, x - 10, y - 10, null);
    }
    /*private void drawTeammateLocation(Canvas canvas, MapView mapView) {
            LiveLocationManager manager = LiveLocationManager
                    .getLiveLocationManager();
            Iterator<Location> itr = manager.getLocationReceivePacketList().iterator();
            while (itr.hasNext()) {
                int markerID = R.drawable.star;

                Location location = itr.next();
                Point screenPt = new Point();
                GeoPoint memberPoint = new GeoPoint(Math.round((int) (location
                        .getLatitude() * 1e6)),
                        Math.round((int) (location.getLongitude() * 1e6)));
                mapView.getProjection().toPixels(memberPoint, screenPt);

                if (OverlayUtils.isInsideCanvas(canvas, screenPt.x, screenPt.y)) {
                    OverlayUtils.drawMarker(canvas, markerID, resources,
                            screenPt.x, screenPt.y);
                } else {
                    OverlayUtils.drawRadar(canvas, R.drawable.arrow, resources,
                            screenPt.x, screenPt.y, memberPoint);
                }
            }
        }
    */
    /*private void drawOwnLocation(Canvas canvas, MapView mapView) {
         LiveLocationManager manager = LiveLocationManager
                 .getLiveLocationManager();

         Point screenPts = new Point();
         GeoPoint ownGeoPoint = LocationUtils.getGeopointFromLocation(manager
                 .getOwnLocation());

         mapView.getProjection().toPixels(ownGeoPoint, screenPts);
         if (OverlayUtils.isInsideCanvas(canvas, screenPts.x, screenPts.y)) {
             OverlayUtils.drawMarker(canvas, R.drawable.starown, resources,
                     screenPts.x, screenPts.y);
         } else {
             OverlayUtils.drawRadar(canvas, R.drawable.arrow, resources,
                     screenPts.x, screenPts.y, ownGeoPoint);
         }
     }*/

    @Override
    public void onListen(Packet packet) {
        if (packet instanceof TeamMemberLocationPacket) {
            Utility.debug(this.getClass().getName(), "location update for overlay received");
            this.teammateLocationPacket = (TeamMemberLocationPacket) packet;
            Toast.makeText(Configuration.getInstance().getCurrentContext(), "teammate overlay:"+teammateLocationPacket.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
}
